package com.xm.xmapp.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.R;
import com.xm.xmapp.ShortTimeOrderDetailsActivity;
import com.xm.xmapp.adapter.TimeOrderListAdapter;
import com.xm.xmapp.adapter.TimeOrderListAdapter.OnClickAble;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type.F_ShortTimeOrder;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.customview.PinJiaTipDialog;
import com.xm.xmapp.customview.XListView;
import com.xm.xmapp.customview.XListView.IXListViewListener;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.request.bean.Get_user_order_listRequest;
import com.xm.xmapp.response.bean.ResponseUserOrderList;
import com.xm.xmapp.response.bean.OrderBeanResponse;

public class ShortTimeOrderFragment extends BaseFragment {

	private XListView xListView;
	private RelativeLayout rel;
	private Get_user_order_listRequest requestBean;
	private ArrayList<OrderBeanResponse> list;
	private OnClickAble interFace;
	private int page;
	private int type;
	private TimeOrderListAdapter adapter;

	public ShortTimeOrderFragment() {
		super();
	}

	public void setType(int type) {
		this.type = type;
	}


	@Override
	public int setContentView() {
		return R.layout.fragment_order;
	}

	@Override
	public void initView() {

		isSuppertLazyLoad = false;
		xListView = (XListView) findViewById(R.id.xListView);
		rel = (RelativeLayout) findViewById(R.id.rel);
	}

	@Override
	public void initData() {

		interFace = new OnClickAble() {

			@Override
			public void setTextView(final View v, final int positon) {

				new PinJiaTipDialog(getActivity(), list.get(positon), new MyDialogButton() {

					@SuppressWarnings("deprecation")
					@Override
					public void setTitle(int flag, String str) {

						TextView status = (TextView) v;
						status.setText("已还车");
						list.get(positon).is_comment = "1";
						status.setOnClickListener(null);
						status.setTextColor(ContextCompat.getColor(getActivity(), R.color.color6));
						status.setBackgroundDrawable(null);

					}
				}).show();

			}

			@Override
			public Object DoSomeThing(Object data) {
				OrderBeanResponse bean = (OrderBeanResponse) data;
				Intent intent = new Intent(MyAppcation.getMyAppcation(), ShortTimeOrderDetailsActivity.class);
				intent.putExtra("orderbean", bean);
				startActivity(intent);
				return null;
			}
		};

		requestBean = new Get_user_order_listRequest();
		requestBean.type = type;
		page = 1;
		requestBean.pagesize = CodeConstant.pageSize;
		list = new ArrayList<OrderBeanResponse>();
		adapter = new TimeOrderListAdapter(list, getActivity(), R.layout.item_short_time_order);
		adapter.setInterFace(interFace);
		xListView.setAdapter(adapter);
		xListView.setPullRefreshEnable(true);
		xListView.setPullLoadEnable(true);
		promptDialog.show();
		getData();
	}

	private void getData() {

		requestBean.page = page;
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				xListView.stopRefresh();
				xListView.stopLoadMore();
				promptDialog.dismiss();
				ResponseUserOrderList base = JSON.parseObject(response, ResponseUserOrderList.class);
				if (page == 1)
					list.clear();

				list.addAll(base.list);
				if (base.list == null || base.list.size() == 0) {
					xListView.setPullLoadEnable(false);
				}

				if (list.size() > 0) {
					xListView.setVisibility(View.VISIBLE);
					rel.setVisibility(View.GONE);
				} else {
					xListView.setVisibility(View.GONE);
					rel.setVisibility(View.VISIBLE);
				}
				adapter.notifyDataSetChanged();
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				promptDialog.dismiss();
				xListView.stopRefresh();
				xListView.stopLoadMore();
			}
		});

	}

	@Override
	public void initEvent() {
		xListView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				xListView.setPullLoadEnable(true);
				page = 1;
				getData();
			}

			@Override
			public void onLoadMore() {
				page++;
				getData();
			}
		});
	}

	/**
	 * AddCommentActivity调用
	 */
	@Override
	public Object dataChang(int flag, Object data) {

		switch (flag) {
		case F_ShortTimeOrder.DEFAULT:
			page = 1;
			promptDialog.show();
			getData();
			break;
		default:
			break;
		}
		return super.dataChang(flag, data);
	}
}
